#include <stdio.h>

int main()
{
	int a[20][20];
	int n, k, i, j;

        // n has to be lower than 20 and also odd. The maximum number
        // with this property is 19. Using a bigger number than 19
        // will destroy the output (because the matrix will be too
        // big).
	do
	{
		printf("Enter n < 19, odd: ");
		scanf("%d", &n);
	} while (n % 2 == 0 || n > 20);

        // Build the magic square. Here's the logic that I used:
        // We populate the magic square (matrix) with elements, starting 
        // from 1 and finishing with n^2. The next position, (i, j) is
        // established based on 'k' value, which is going to be placed in
        // matrix: if k mod n equals to 1, then the next position will have 
        // the row 'j' smaller with one position compared with the current
        // position. Otherwise, both 'i' and 'j' are incremented by one.
        // The initial position will be: i0 = (n + 1) / 2 and j0 = n + 1.
	i = (n + 1) / 2;
	j = n + 1;
	for (k = 1; k <= n * n; k++)
	{
		if (k % n == 1)
			j--;
		else
		{
			i++;
			if (i > n)
				i = 1;
			j++;
			if (j > n)
				j = 1;
		}
		a[i][j] = k;
	}

        // Output the magic square. This is simply the algorithm 
        // used to output a matrix, but I have added some ASCII "art".
	printf("+---");
	for (j = 1; j < n; j++)
		printf("----");
	printf("+\n");
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= n; j++)
			printf("|%3d", a[i][j]);
		printf("|\n");
		if (i < n)
		{
			printf("|---");
			for (j = 1; j < n; j++)
				printf("+---");
			printf("|\n");
		}
		else
		{
			printf("+---");
			for (j = 1; j < n; j++)
				printf("----");
			printf("+\n");
		}
	}

	return 0;
}